package com.lbw.Client.serviceCenter.ZkWatcher;


import com.lbw.Client.cache.serviceCache;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.CuratorCache;

/**
 * @author lbwxxc
 * @date 2025/2/21 17:27
 * @description: TODO
 */
public class watchZK {
    private CuratorFramework client;
    serviceCache cache;

    public watchZK(CuratorFramework client,serviceCache  cache){
        this.client=client;
        this.cache=cache;
    }

    public void watchToUpdate(String path) throws InterruptedException {
        CuratorCache curatorCache = CuratorCache.build(client, "/");
        curatorCache.listenable().addListener((type, childData, childData1) -> {

            switch (type.name()) {
                case "NODE_CREATED":
                    String[] pathList = parsePath(childData1);
                    if (pathList.length <= 2) {
                        break;
                    } else {
                        String serviceName = pathList[1];
                        String address = pathList[2];
                        cache.addServiceToCache(serviceName, address);
                    }
                    break;
                case "NODE_CHANGED":
                    if (childData.getData() != null) {
                        System.out.println("修改前的数据: " + new String(childData.getData()));
                    } else {
                        System.out.println("节点第一次赋值!");
                    }
                    String[] oldPathList=parsePath(childData);
                    String[] newPathList=parsePath(childData1);
                    cache.replaceServiceAddress(oldPathList[1],oldPathList[2],newPathList[2]);
                    System.out.println("修改后的数据: " + new String(childData1.getData()));
                    break;
                case "NODE_DELETED":
                    String[] pathList_d= parsePath(childData);
                    if(pathList_d.length<=2) break;
                    else {
                        String serviceName=pathList_d[1];
                        String address=pathList_d[2];
                        //将新注册的服务加入到本地缓存中
                        cache.delete(serviceName,address);
                    }
                    break;
                default:
                    break;
            }
        });
        curatorCache.start();
    }

    public String[] parsePath(ChildData childData){
        String path = childData.getPath();
        return path.split("/");
    }
}
